﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.LogicalGroups.CApLGRExcelReportManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.LogicalGroups;

internal class CApLGRExcelReportManager
{
  private Application m_excelApp;
  private _Workbook m_workBook;
  private _Worksheet m_workSheet;
  private CultureInfo m_oldCultureInfo;
  private int m_rowIndex = 2;
  private int m_maxColumn = 4;
  private string m_maxColumnLetter = "D";
  private const int m_numberColumn = 1;
  private const string m_numberColumnName = "A1";
  private const int m_objectColumn = 2;
  private const string m_objectColumnName = "B1";
  private const int m_levelColumn = 3;
  private const string m_levelColumnName = "C1";
  private const int m_groupHierarchyColumn = 4;
  private const string m_groupHierarchyColumnName = "D1";
  private const int m_fieldsOffset = 4;

  internal CApLGRExcelReportManager()
  {
    this.m_excelApp = (Application) null;
    this.m_workBook = (_Workbook) null;
    this.m_workSheet = (_Worksheet) null;
    this.m_oldCultureInfo = Thread.CurrentThread.CurrentCulture;
  }

  public bool InitReport(int columnsNumber)
  {
    bool flag = false;
    try
    {
      this.m_maxColumn = columnsNumber + 4;
      this.m_maxColumnLetter = this.GetExcelColumnName(this.m_maxColumn);
      this.SetThreadCultureInfo(new CultureInfo("en-US"));
      if (this.m_excelApp == null)
      {
        this.m_excelApp = (Application) Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
        this.m_excelApp.UserControl = true;
      }
      else if (this.m_workBook != null)
      {
        // ISSUE: reference to a compiler-generated method
        this.m_workBook.Close((object) false, Type.Missing, Type.Missing);
        this.m_workBook = (_Workbook) null;
      }
      // ISSUE: reference to a compiler-generated method
      this.m_workBook = (_Workbook) this.m_excelApp.Workbooks.Add((object) Missing.Value);
      if (this.m_workBook != null)
      {
        this.m_workSheet = this.m_workBook.Sheets[(object) 1] as _Worksheet;
        if (this.m_workSheet != null)
        {
          this.m_workSheet.Name = LGRLogicalGroupResource.EXPORT_GROUPS_WORKSHEET_NAME;
          // ISSUE: reference to a compiler-generated method
          this.m_workSheet.get_Range((object) "B1", (object) "B11").EntireColumn.VerticalAlignment = (object) XlVAlign.xlVAlignTop;
          // ISSUE: reference to a compiler-generated method
          // ISSUE: reference to a compiler-generated method
          // ISSUE: variable of a compiler-generated type
          Microsoft.Office.Interop.Excel.Range entireColumn = this.m_workSheet.get_Range((object) "A1", (object) "A1").get_Offset((object) 0, (object) 2).EntireColumn;
          // ISSUE: reference to a compiler-generated method
          entireColumn.Activate();
          entireColumn.Application.ActiveWindow.FreezePanes = true;
          flag = true;
        }
      }
    }
    catch (COMException ex)
    {
    }
    return flag;
  }

  public bool SetVisibility(bool visibilityStatus)
  {
    bool flag = false;
    if (this.m_excelApp != null)
    {
      this.m_excelApp.Visible = visibilityStatus;
      flag = visibilityStatus.Equals(this.m_excelApp.Visible);
    }
    return flag;
  }

  public bool SetCellValue(int row, int column, object value)
  {
    bool flag = false;
    if (value != null && this.m_workSheet != null)
    {
      if (this.m_workSheet.Cells[(object) row, (object) column] is Microsoft.Office.Interop.Excel.Range cell)
      {
        cell.Value2 = value;
        flag = true;
      }
      Marshal.ReleaseComObject((object) cell);
    }
    return flag;
  }

  public bool AddObject(object value, string hierarchyName, int indent, bool bold)
  {
    bool flag = this.SetCustomCellValue(this.m_rowIndex, 2, value, indent, bold);
    if (flag)
    {
      this.SetCellValue(this.m_rowIndex, 3, (object) (indent + 1));
      this.SetCellValue(this.m_rowIndex, 4, (object) hierarchyName);
      ++this.m_rowIndex;
    }
    return flag;
  }

  public void Release()
  {
    if (this.m_workSheet != null)
    {
      Marshal.ReleaseComObject((object) this.m_workSheet);
      this.m_workSheet = (_Worksheet) null;
    }
    if (this.m_workBook != null)
    {
      Marshal.ReleaseComObject((object) this.m_workBook);
      this.m_workBook = (_Workbook) null;
    }
    if (this.m_excelApp != null)
    {
      Marshal.ReleaseComObject((object) this.m_excelApp);
      this.m_excelApp = (Application) null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    this.ResetThreadCultureInfo();
  }

  public void Quit()
  {
    if (this.m_excelApp != null)
    {
      if (this.m_workSheet != null)
      {
        Marshal.ReleaseComObject((object) this.m_workSheet);
        this.m_workSheet = (_Worksheet) null;
      }
      if (this.m_workBook != null)
      {
        // ISSUE: reference to a compiler-generated method
        this.m_workBook.Close((object) false, (object) Missing.Value, (object) Missing.Value);
        Marshal.ReleaseComObject((object) this.m_workBook);
        this.m_workBook = (_Workbook) null;
      }
      // ISSUE: reference to a compiler-generated method
      this.m_excelApp.Workbooks.Close();
      // ISSUE: reference to a compiler-generated method
      this.m_excelApp.Quit();
      Marshal.ReleaseComObject((object) this.m_excelApp);
      this.m_excelApp = (Application) null;
      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();
    }
    this.ResetThreadCultureInfo();
  }

  public void WriteHeader(List<CApLGRExcelReportColumnData> columnList)
  {
    int row = 1;
    this.SetCellValue(row, 1, (object) LGRLogicalGroupResource.EXPORT_GROUPS_NUMBER_COLUMN);
    this.SetCellValue(row, 2, (object) LGRLogicalGroupResource.EXPORT_GROUPS_OBJECT_COLUMN);
    this.SetCellValue(row, 3, (object) LGRLogicalGroupResource.EXPORT_GROUPS_LEVEL_COLUMN);
    this.SetCellValue(row, 4, (object) LGRLogicalGroupResource.EXPORT_GROUPS_HIERARCHY_COLUMN);
    foreach (CApLGRExcelReportColumnData column in columnList)
      this.SetCellValue(row, column.Index + 4, (object) column.Name);
    // ISSUE: reference to a compiler-generated method
    // ISSUE: variable of a compiler-generated type
    Microsoft.Office.Interop.Excel.Range range = this.m_workSheet.get_Range((object) "A1", (object) (this.m_maxColumnLetter + "1"));
    range.Font.Bold = (object) true;
    range.Font.Size = (object) 12;
    range.Borders.LineStyle = (object) XlLineStyle.xlContinuous;
    range.Borders.Weight = (object) XlBorderWeight.xlThin;
    range.Borders.Color = (object) ColorTranslator.ToOle(Color.Black);
    // ISSUE: reference to a compiler-generated method
    range.EntireRow.AutoFit();
  }

  public void WriteAttributes(
    TxPlanningFieldExtendedDataDictionary data,
    List<CApLGRExcelReportColumnData> typeColumns)
  {
    foreach (CApLGRExcelReportColumnData typeColumn in typeColumns)
    {
      if (data.ContainsFieldName(typeColumn.FieldName))
      {
        TxPlanningFieldData fieldValue1 = data.FieldExtendedData(typeColumn.FieldName).FieldValue;
        if (fieldValue1.FieldValue != null)
        {
          object fieldValue2 = fieldValue1.FieldValue;
          if (fieldValue2.GetType() == typeof (TxTransformation))
          {
            string str = CApLGRLogicalGroupsUtil.ConvertTransformationToString(fieldValue2 as TxTransformation);
            this.m_workSheet.Cells[(object) (this.m_rowIndex - 1), (object) (typeColumn.Index + 4)] = (object) str;
          }
          else if (fieldValue2.GetType() == typeof (ArrayList))
          {
            string str = CApLGRLogicalGroupsUtil.ConvertArrayListToString(fieldValue2 as ArrayList);
            this.m_workSheet.Cells[(object) (this.m_rowIndex - 1), (object) (typeColumn.Index + 4)] = (object) str;
          }
          else
            this.m_workSheet.Cells[(object) (this.m_rowIndex - 1), (object) (typeColumn.Index + 4)] = fieldValue1.FieldValue;
        }
      }
    }
  }

  public void Allign()
  {
    this.ApplyNumbering();
    int num = this.m_rowIndex - 1;
    // ISSUE: reference to a compiler-generated method
    // ISSUE: variable of a compiler-generated type
    Microsoft.Office.Interop.Excel.Range range1 = this.m_workSheet.get_Range((object) "A1", (object) ("A" + num.ToString()));
    for (int ColumnOffset = 0; ColumnOffset < this.m_maxColumn; ++ColumnOffset)
    {
      // ISSUE: reference to a compiler-generated method
      // ISSUE: reference to a compiler-generated method
      range1.get_Offset((object) 0, (object) ColumnOffset).BorderAround((object) XlLineStyle.xlContinuous, ColorIndex: XlColorIndex.xlColorIndexNone, Color: (object) ColorTranslator.ToOle(Color.Black));
    }
    // ISSUE: reference to a compiler-generated method
    // ISSUE: variable of a compiler-generated type
    Microsoft.Office.Interop.Excel.Range range2 = this.m_workSheet.get_Range((object) "B1", (object) (this.m_maxColumnLetter + num.ToString()));
    // ISSUE: reference to a compiler-generated method
    range2.Columns.AutoFit();
    // ISSUE: reference to a compiler-generated method
    this.m_workSheet.get_Range((object) "C1", (object) (this.m_maxColumnLetter + num.ToString())).VerticalAlignment = (object) XlVAlign.xlVAlignTop;
    // ISSUE: reference to a compiler-generated method
    this.m_workSheet.get_Range((object) "C1", (object) (this.m_maxColumnLetter + num.ToString())).HorizontalAlignment = (object) XlHAlign.xlHAlignLeft;
    // ISSUE: reference to a compiler-generated field
    if (CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__0 == null)
    {
      // ISSUE: reference to a compiler-generated field
      CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__0 = CallSite<Func<CallSite, object, Microsoft.Office.Interop.Excel.Range>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof (Microsoft.Office.Interop.Excel.Range), typeof (CApLGRExcelReportManager)));
    }
    // ISSUE: reference to a compiler-generated field
    // ISSUE: reference to a compiler-generated field
    // ISSUE: variable of a compiler-generated type
    Microsoft.Office.Interop.Excel.Range range3 = CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__0.Target((CallSite) CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__0, this.m_workSheet.Cells[(object) 1, (object) 2]);
    range3.IndentLevel = (object) 0;
    // ISSUE: reference to a compiler-generated field
    if (CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__1 == null)
    {
      // ISSUE: reference to a compiler-generated field
      CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__1 = CallSite<Func<CallSite, object, Microsoft.Office.Interop.Excel.Range>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof (Microsoft.Office.Interop.Excel.Range), typeof (CApLGRExcelReportManager)));
    }
    // ISSUE: reference to a compiler-generated field
    // ISSUE: reference to a compiler-generated field
    // ISSUE: variable of a compiler-generated type
    Microsoft.Office.Interop.Excel.Range range4 = CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__1.Target((CallSite) CApLGRExcelReportManager.\u003C\u003Eo__25.\u003C\u003Ep__1, this.m_workSheet.Cells[(object) 1, (object) 1]);
    // ISSUE: reference to a compiler-generated method
    range4.Select();
  }

  private void ResetThreadCultureInfo()
  {
    Thread.CurrentThread.CurrentCulture = this.m_oldCultureInfo;
  }

  private void SetThreadCultureInfo(CultureInfo newCultureInfo)
  {
    Thread.CurrentThread.CurrentCulture = newCultureInfo;
  }

  private void ApplyNumbering()
  {
    for (int row = 2; row < this.m_rowIndex; ++row)
      this.SetCellValue(row, 1, (object) (row - 1));
    // ISSUE: reference to a compiler-generated method
    // ISSUE: variable of a compiler-generated type
    Microsoft.Office.Interop.Excel.Range range = this.m_workSheet.get_Range((object) "A1", (object) ("A" + (this.m_rowIndex - 1).ToString()));
    range.Font.Color = (object) ColorTranslator.ToOle(Color.Gray);
    range.VerticalAlignment = (object) XlVAlign.xlVAlignTop;
    range.HorizontalAlignment = (object) XlHAlign.xlHAlignLeft;
    range.EntireColumn.ColumnWidth = (object) 3;
  }

  private bool SetCustomCellValue(
    int rowIndex,
    int colIndex,
    object value,
    int indent,
    bool bold)
  {
    bool flag = false;
    if (value != null && this.m_workSheet != null)
    {
      if (this.m_workSheet.Cells[(object) rowIndex, (object) colIndex] is Microsoft.Office.Interop.Excel.Range cell)
      {
        cell.Value2 = value;
        if (indent > 15)
          indent = 15;
        cell.IndentLevel = (object) indent;
        cell.Font.Bold = (object) bold;
        flag = true;
      }
      Marshal.ReleaseComObject((object) cell);
    }
    return flag;
  }

  private string GetExcelColumnName(int columnNumber)
  {
    int num1 = columnNumber;
    string excelColumnName = string.Empty;
    int num2;
    for (; num1 > 0; num1 = (num1 - num2) / 26)
    {
      num2 = (num1 - 1) % 26;
      excelColumnName = Convert.ToChar(65 + num2).ToString() + excelColumnName;
    }
    return excelColumnName;
  }
}
